﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Configuration;

namespace GlxRRSMain.Utils
{
    public static class FileHelper
    {
        public const string EXT_XLS = ".xls";
        private static string x_currentDir = null;
        private static string x_Template = null;
        private static string x_XlsConnectionFormat = null;
        //private const string XLS_CONN_FORMAT = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='{0}';Extended Properties='Excel 12.0 xml;HDR=YES;IMEX=1';";

        public static string XlsConnectionFormat
        {
            get
            {
                if (x_XlsConnectionFormat == null)
                {
                    // read from config file
                    //Map the roaming configuration file. This enables the application to access the configuration file using the 
                    //System.Configuration.Configuration class 
                    ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
                    configFileMap.ExeConfigFilename = "xls.config";
                    // Get the mapped configuration file. 
                    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
                    x_XlsConnectionFormat = config.AppSettings.Settings["provider"].Value;
                    //config.Save();
                }
                return x_XlsConnectionFormat;
            }
            set
            {
                x_XlsConnectionFormat = value;
            }
        }
        private static string CurrentDir
        {
            get
            {
                if (x_currentDir == null)
                {
                    //string startupPath = System.IO.Directory.GetCurrentDirectory();
                    x_currentDir = Environment.CurrentDirectory;
                }
                return x_currentDir;
            }
        }
        private static string TemplateDir
        {
            get
            {
                if (x_Template == null)
                {
                    //string startupPath = System.IO.Directory.GetCurrentDirectory();
                    x_Template = CurrentDir + "\\templates\\";
                }
                return x_Template;
            }
        }
        //
        public static string getDailyReportTemplate()
        {
            return TemplateDir + "DailyReportTemplate.xls";
        }

        public static string genDailyReportOut()
        {
            DateTime dt = DateTime.Now;
            return CurrentDir + "\\out\\DailyReportTemplate_" + dt.Ticks + EXT_XLS;
        }
        //
        public static string getMonthlyReportByCinemaTemplate()
        {
            return TemplateDir + "MonthlyReportByCinemaTemplate.xls";
        }

        public static string genMonthlyReportByCinemaTemplateOut()
        {
            DateTime dt = DateTime.Now;
            return CurrentDir + "\\out\\MonthlyReportByCinemaTemplate" + dt.Ticks + EXT_XLS;
        }
        //
        public static string getTicketReportByCinemaTemplate()
        {
            return TemplateDir + "TicketReportByCinemaTemplate.xls";
        }

        public static string genTicketReportByCinemaTemplateOut()
        {
            DateTime dt = DateTime.Now;
            return CurrentDir + "\\out\\TicketReportByCinemaTemplate" + dt.Ticks + EXT_XLS;
        }
        //
        public static string getWeeklyReportByCinemaTemplate()
        {
            return TemplateDir + "WeeklyReportByCinemaTemplate.xls";
        }

        public static string genWeeklyReportByCinemaTemplateOut()
        {
            DateTime dt = DateTime.Now;
            return CurrentDir + "\\out\\WeeklyReportByCinemaTemplate" + dt.Ticks + EXT_XLS;
        }
        //
        public static string getWeeklyReportByFilmTemplate()
        {
            return TemplateDir + "WeeklyReportByFilmTemplate.xls";
        }

        public static string genWeeklyReportByFilmTemplateOut()
        {
            DateTime dt = DateTime.Now;
            return CurrentDir + "\\out\\WeeklyReportByFilmTemplate" + dt.Ticks + EXT_XLS;
        }

        public static void cleanExportData()
        {
            System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(CurrentDir + "\\out");
            if (directory.Exists)
            {
                directory.Empty();
            }
            else
            {
                directory.Create();
            }
        }

        public static string getExcelConnectionString(string path)
        {
            string a = XlsConnectionFormat;
            return string.Format(a, path);
        }

        public static System.Data.DataSet getDataSetFromExcel(string path)
        {
            System.Data.OleDb.OleDbConnection MyConnection;
            System.Data.OleDb.OleDbDataAdapter MyCommand;
            string conn = FileHelper.getExcelConnectionString(path);
            MyConnection = new System.Data.OleDb.OleDbConnection(conn);
            MyConnection.Open();
            MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
            MyCommand.TableMappings.Add("Table", "TestTable");
            System.Data.DataSet DtSet = new System.Data.DataSet();
            MyCommand.Fill(DtSet);
            MyConnection.Close();
            //
            return DtSet;
        }

    }
}
